<template>
  <div class="container">
    <el-card>
      <el-form :inline="true" class="demo-form-inline">
        <el-row type="flex" justify="space-between">
          <el-col :span="7">
            <el-form-item label="关键字" label-width="80px">
              <el-input
                v-model="searchObj.keyword"
                placeholder="根据编号搜索"
                style="width: 335px"
              ></el-input>
              <el-form-item> </el-form-item>
            </el-form-item>
          </el-col>

          <el-col :span="3">
            <el-form-item>
              <el-button @click="cleanUp">清除</el-button>
              <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-alert
          :title="`数据一共${total}条`"
          type="info"
          show-icon
          :closable="false"
          style="margin-bottom: 15px"
        >
        </el-alert>
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="id" label="编号" width="200">
          </el-table-column>
          <el-table-column prop="questionType" label="题型" width="90">
          </el-table-column>
          <el-table-column prop="questionIDs" label="题目编号">
            <template slot-scope="{ row }">
              <div
                v-for="(item, index) in row.questionIDs"
                :key="index"
                @click="showdialog(row.questionIDs[index].id)"
                style="cursor: pointer"
              >
                <span style="color: rgb(0, 153, 255)">{{ item.number }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="addTime" label="录入时间"> </el-table-column>
          <el-table-column prop="totalSeconds" label="答题时间(S)">
          </el-table-column>
          <el-table-column prop="accuracyRate" label="正确率(%)">
          </el-table-column>
          <el-table-column prop="userName" label="录入人"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="delQuestions(row.id)"
              ></el-button></template
          ></el-table-column>
        </el-table>
      </el-form>
      <el-row type="flex" justify="center">
        <el-col :span="8" style="margin: 20px 0 0">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="searchObj.page"
            :page-sizes="[20, 30, 50, 100]"
            :page-size="searchObj.pagesize"
            layout="prev,pager,next,sizes,jumper"
            :total="total"
          >
          </el-pagination>
        </el-col> </el-row
      >""
    </el-card>
    <QuestionsPreview
      :dialogVisible.sync="showDialog"
      :previewlist="previewlist"
    />
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions.js'
import QuestionsPreview from '../components/questions-preview'
export default {
  components: {
    QuestionsPreview
  },
  data() {
    return {
      previewlist: {},
      showDialog: false,
      searchObj: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      total: 0,
      questionType: ['单选', '多选', '简答'],
      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1516 弄'
      }]
    }
  },
  methods: {
    onSubmit() {
      this.getRandomsList(this.searchObj)
    },
    cleanUp() {
      this.searchObj.keyword = ''
      this.getRandomsList(this.searchObj)
    },
    async getRandomsList(data = this.searchObj) {
      const res = await randoms(data)
      const list = res.data.items
      this.total = res.data.counts
      list.forEach(item => {
        item.questionType = this.questionType[item.questionType - 1]
      })
      this.tableData = list
    },
    async showdialog(id) {
      const res = await detail({ id: id })
      this.previewlist = res.data
      this.showDialog = true
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.searchObj.pagesize = val
      this.getRandomsList(this.searchObj)
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.searchObj.page = val
      this.getRandomsList(this.searchObj)
    },
    delQuestions(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await removeRandoms({ id })
        this.getRandomsList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
  created () {
    this.getRandomsList(this.searchObj)
  }

}
</script>

<style scoped >
.container {
  margin: 20px 20px;
  background-color: #fff;
}
</style>
